﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using dhDataAcess.produceDAL;
using dhSystemBLL.produceBLL;
using dhSystemBLL.orderData;
using dhDataAcess.produceLinq;
using baseSet.importExport;
using dhDataAcess.baseBLL;

namespace dhsystem.produceForm
{
    public partial class FrmpackStockInDetailAdd : Form
    {
        packStockInBLL packStockIn = new packStockInBLL();
        packStockInDetailModel packStockInDetailRow = new packStockInDetailModel();
        productListBLL product = new productListBLL();
        orderDetailBLL orderDetail = new orderDetailBLL();
        BindingSource BindingSourcepackStockInDetailAdd;
        string state;
        string billNo;
        //绑定数据源
        public FrmpackStockInDetailAdd(BindingSource bindingSourcepackStockInDetail, string _state, string _billNo)
        {
            InitializeComponent();
            BindingSourcepackStockInDetailAdd = bindingSourcepackStockInDetail;
            state = _state;
            billNo = _billNo;
        }
        //打开窗口时绑定数据
        private void FrmpackStockInDetailAdd_Load(object sender, EventArgs e)
        {
            //bindingSourceproductList.DataSource = product.getproductListAll();
            //comboBoxpName.DataSource = bindingSourceproductList;
            //comboBoxpName.DisplayMember = "pname";
            //comboBoxpName.ValueMember = "pname";
            //bindingSourceorderDetail.DataSource = orderDetail.getorderDetailAll();
            //comboBoxorderDetailNo.DataSource = bindingSourceorderDetail;
            //comboBoxorderDetailNo.DisplayMember = "orderDetailNo";
            //comboBoxorderDetailNo.ValueMember = "orderDetailNo";

            if (state == "编辑")
            {
                textbillNo.DataBindings.Add("text", BindingSourcepackStockInDetailAdd, "billNo");
                textpName.DataBindings.Add("text", BindingSourcepackStockInDetailAdd, "pName");
                textorderDetailNo.DataBindings.Add("text", BindingSourcepackStockInDetailAdd, "orderDetailNo");
                textqty.DataBindings.Add("text", BindingSourcepackStockInDetailAdd, "qty");
                textunit.DataBindings.Add("text", BindingSourcepackStockInDetailAdd, "unit");
                textboxno.DataBindings.Add("text", BindingSourcepackStockInDetailAdd, "boxno");
                textqtywc.DataBindings.Add("text", BindingSourcepackStockInDetailAdd, "qtywc");
                textqtysy.DataBindings.Add("text", BindingSourcepackStockInDetailAdd, "qtysy");
                textJweight.DataBindings.Add("text", BindingSourcepackStockInDetailAdd, "Jweight");
                textMweight.DataBindings.Add("text", BindingSourcepackStockInDetailAdd, "Mweight");
                textmark.DataBindings.Add("text", BindingSourcepackStockInDetailAdd, "mark");
                //textid.DataBindings.Add("text", BindingSourcescrlDetailAdd, "recordid");
                // BindingSourceDetailAdd.AddNew();
                //textproductname.Text = productname;
            }
            if (state == "新增")
            {
                textbillNo.DataBindings.Add("text", BindingSourcepackStockInDetailAdd, "billNo");
                textpName.DataBindings.Add("text", BindingSourcepackStockInDetailAdd, "pName");
                textorderDetailNo.DataBindings.Add("text", BindingSourcepackStockInDetailAdd, "orderDetailNo");
                textqty.DataBindings.Add("text", BindingSourcepackStockInDetailAdd, "qty");
                textunit.DataBindings.Add("text", BindingSourcepackStockInDetailAdd, "unit");
                textboxno.DataBindings.Add("text", BindingSourcepackStockInDetailAdd, "boxno");
                textqtywc.DataBindings.Add("text", BindingSourcepackStockInDetailAdd, "qtywc");
                textqtysy.DataBindings.Add("text", BindingSourcepackStockInDetailAdd, "qtysy");
                textJweight.DataBindings.Add("text", BindingSourcepackStockInDetailAdd, "Jweight");
                textMweight.DataBindings.Add("text", BindingSourcepackStockInDetailAdd, "Mweight");
                textmark.DataBindings.Add("text", BindingSourcepackStockInDetailAdd, "mark");
                // textid.DataBindings.Add("text", BindingSourcescrlDetailAdd, "recordid");
                BindingSourcepackStockInDetailAdd.AddNew();
                textbillNo.Text = billNo;
            }

            textqty.Focus();
        }
        //保存按钮，插入或修改数据
        private void buttonOK_Click(object sender, EventArgs e)
        {
            try
            {
                DialogResult result = MessageBox.Show("是否保存资料！", "提示信息", MessageBoxButtons.YesNo);
                if (result == DialogResult.Yes)
                {

                    BindingSourcepackStockInDetailAdd.EndEdit(); //编辑到完成，写到数据源
                    BindingSourcepackStockInDetailAdd.CancelEdit(); //刷新数据作用

                }
            }

            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
        }
        //取消按钮
        private void buttonCancel_Click(object sender, EventArgs e)
        {
            BindingSourcepackStockInDetailAdd.CancelEdit();
            this.Close();
        }
        //选择漂染单号
        private void buttworkerselect_Click(object sender, EventArgs e)
        {
            string field;
            field = "orderDetailNo";
            Frmprodutselect productselect = new Frmprodutselect(moduleInfo.orderDetail , field);
            DialogResult result = productselect.ShowDialog();
            if (result == DialogResult.Yes)
            {
                textorderDetailNo.Text  = productselect.pname;
            }
        }
        //漂染单号值改变之后
        private void textorderDetailNo_TextChanged(object sender, EventArgs e)
        {
            textpName.Text = basicSql.getpnamebyorderDetail(textorderDetailNo.Text);
            if (basicSql.getstockQtyKG(textpName.Text) == "")
            {
                textqty.Text = "0";
                textqtysy.Text = Convert.ToString(Convert.ToDouble(basicSql.getqtybyorderDetail(textorderDetailNo.Text)));
            }
            else
            {
                textqtywc.Text = basicSql.getstockQtyKG(textpName.Text);
                textqtysy.Text = Convert.ToString(Convert.ToDouble(basicSql.getqtybyorderDetail(textorderDetailNo.Text)) - Convert.ToDouble(textqtywc.Text));
            }
            textunit.Text = basicSql.getunitbyorderDetail(textorderDetailNo.Text);
            //textqtysy.Text = Convert.ToString(Convert.ToDouble (basicSql.getqtybyorderDetail(textorderDetailNo.Text)) - Convert .ToDouble (textqtywc.Text)); //-Convert .ToDecimal ( basicSql.getSendQty(textorderDetailNo.Text)));
            //bindingSourcepdfpjlb.DataSource = pdfpjlb.getpdfpjlbAllByorderDetailNo(textorderDetailNo.Text);
            //dGridorderDetail.DataSource = bindingSourcepdfpjlb;

            //DcolumnSet.colSet(moduleInfo.pdfpjlb, dGridorderDetail);
        }

        private void textJweight_TextChanged(object sender, EventArgs e)
        {
            try
            {
                //根据锣重转换成订单 需要的单位数
            }
            catch { }
        }
    }
}
